package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.WashReport;
import com.tianxin.workbao.workBao.holder.EmptyHolder;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SelectedValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by hacker on 2017/7/26.
 */

public class WashReportAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_HEADER = 1;
    private static final int TYPE_BODY = 2;
    private List<WashReport> washReportList;
    private Context mContext;

    public WashReportAdapter(List<WashReport> list, Context context) {
        this.washReportList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (washReportList.size() > 0) {
            if (position == 0) {
                return TYPE_HEADER;
            } else {
                return TYPE_BODY;
            }
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_HEADER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.wash_header_item, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_BODY) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.wash_body_item, parent, false);
            return new BodyViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.BindViewData(washReportList.get(position));
            headerViewHolder.setViewListener(position);
        } else if (holder instanceof BodyViewHolder) {
            BodyViewHolder bodyViewHolder = (BodyViewHolder) holder;
            bodyViewHolder.BindViewData(washReportList.get(position));
            bodyViewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (washReportList.size() > 0) {
            return washReportList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
            case TYPE_BODY:
            case TYPE_EMPTY:
                return 1;
            default:
                return 0;
        }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {
        CircleImageView nestBtn;
        PieChartView pieChartView;
        TextView inWeight;
        TextView outWeight;
        TextView differWeight;
        private int lastPosition = -1;
        private int basePosition;
        private List<SliceValue> sliceValueList;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            nestBtn = (CircleImageView) itemView.findViewById(R.id.next_btn);
            pieChartView = (PieChartView) itemView.findViewById(R.id.pieChartView);
            inWeight = (TextView) itemView.findViewById(R.id.in_weight);
            outWeight = (TextView) itemView.findViewById(R.id.out_weight);
            differWeight = (TextView) itemView.findViewById(R.id.differ_weight);
        }

        public void BindViewData(final WashReport report) {
            inWeight.setText("入洗 " + report.getInWeight() + " 吨");
            outWeight.setText("产出  " + report.getOutWeight() + " 吨");
            DecimalFormat df = new DecimalFormat("0.00");
            float defferWeight = Float.valueOf(df.format(report.getOutWeight() - report.getInWeight()));
            differWeight.setText("相差 " + df.format(defferWeight));
            sliceValueList = new ArrayList<>();
            SliceValue value1 = new SliceValue();
            value1.setColor(mContext.getResources().getColor(R.color.recycler_color2));
            value1.setLabel("入洗 " + report.getInWeight() + " 吨");
            value1.setValue(report.getInWeight());
            sliceValueList.add(value1);
            SliceValue value2 = new SliceValue();
            value2.setColor(mContext.getResources().getColor(R.color.recycler_color4));
            value2.setLabel("产出 " + report.getOutWeight() + " 吨");
            value2.setValue(report.getOutWeight());
            sliceValueList.add(value2);
            SliceValue value3 = new SliceValue();
            value3.setColor(mContext.getResources().getColor(R.color.recycler_color1));
            value3.setLabel("相差 " + df.format(defferWeight) + " 吨");
            value3.setValue(defferWeight);
            sliceValueList.add(value3);

            PieChartData data = new PieChartData();
            data.setValues(sliceValueList);
            data.setHasCenterCircle(true);
            data.setCenterCircleColor(mContext.getResources().getColor(R.color.write));

            pieChartView.setPieChartData(data);
            pieChartView.setOnValueTouchListener(getValueSelectListener());
            pieChartView.setCircleFillRatio(1);
            pieChartView.setChartRotation(45, false);
            pieChartView.setChartRotationEnabled(true);
        }

        public void setViewListener(final int position) {
            nestBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (lastPosition != -1) {
                        basePosition = lastPosition;
                    }
                    basePosition = (basePosition - 1 + sliceValueList.size()) % sliceValueList.size();
                    SelectedValue selectedValue =
                            new SelectedValue(basePosition, 0, SelectedValue.SelectedValueType.NONE);
                    pieChartView.selectValue(selectedValue);
                }
            });
        }
    }

    class BodyViewHolder extends RecyclerView.ViewHolder {
        TextView inWeight;
        TextView outWeight;
        TextView differWeight;
        TextView timeTag;

        public BodyViewHolder(View itemView) {
            super(itemView);
            inWeight = (TextView) itemView.findViewById(R.id.in_weight);
            outWeight = (TextView) itemView.findViewById(R.id.out_weight);
            differWeight = (TextView) itemView.findViewById(R.id.differ_weight);
            timeTag = (TextView) itemView.findViewById(R.id.add_time);
        }

        public void BindViewData(final WashReport report) {
            inWeight.setText(report.getInWeight() + " 吨");
            outWeight.setText(report.getInWeight() + " 吨");
            DecimalFormat df = new DecimalFormat("0.00");
            float defferWeight = Float.valueOf(df.format(report.getOutWeight() - report.getInWeight()));
            differWeight.setText(df.format(defferWeight));
            timeTag.setText(report.getTimeTag());
        }

        public void setViewListener(final int position) {

        }
    }

    public PieChartOnValueSelectListener getValueSelectListener() {
        PieChartOnValueSelectListener listener = new PieChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int arcIndex, final SliceValue value) {
                com.nispok.snackbar.Snackbar snackbar =
                        Snackbar
                                .with(mContext)
                                .type(SnackbarType.MULTI_LINE)
                                .duration(com.nispok.snackbar.Snackbar.SnackbarDuration.LENGTH_SHORT)
                                .position(com.nispok.snackbar.Snackbar.SnackbarPosition.BOTTOM)
                                .margin(15, 15)
                                .backgroundDrawable(R.color.colorPrimary)
                                .text(String.valueOf(value.getLabel()))
                                .textColor(Color.WHITE)
                                .actionLabel("查看")
                                .actionColor(Color.WHITE)
                                .actionListener(new ActionClickListener() {
                                    @Override
                                    public void onActionClicked(Snackbar snackbar) {
                                        Toast.makeText(mContext, String.valueOf(value.getLabel()), Toast.LENGTH_SHORT).show();
                                    }
                                });

                SnackbarManager.show(snackbar);
            }

            @Override
            public void onValueDeselected() {

            }
        };
        return listener;
    }
}

